# 页面

import PySimpleGUI as sg

from service import Student_Service_Impl as ssi
from service import System_Service_Impl as sysI
from service import User_Service_Impl as usi


# 查看学生列表
def getAllStudents():
    result = ssi.selectAllStudent()
    layout = [
        [sg.Table(result[0], headings=result[1], auto_size_columns=True)],
        [sg.Button('返回首页', key='-return-', size=(40, 3))]
    ]
    # 3) 创建窗口
    window_gp = sg.Window('查看学生信息', layout, grab_anywhere=False)
    # 4) 事件循环
    sysI.outView(window_gp)


# 添加学生信息
def addStudent():
    layout = [[sg.Input('请输入学生姓名', font=('宋体', 20), size=(20, 4), key='-student_name-')],
              [sg.Input('请输入学生性别', font=('宋体', 20), size=(20, 4), key='-student_sex-')],
              [sg.Input('请输入学生年龄', font=('宋体', 20), size=(20, 4), key='-student_age-')],
              [sg.Input('请输入学生地址', font=('宋体', 20), size=(20, 4), key='-student_address-')],
              [sg.B('确认', key='-ok-', size=(15, 2)), sg.B('取消', key='-cancel-', size=(15, 2))]
        , [sg.B('返回首页', key='-return-', size=(32, 2))]]
    window_ap = sg.Window('添加学生信息', layout, resizable=True)
    ssi.insertStudent(window_ap)


# 根据编号删除学生
def deleteStudentById():
    layout = [[sg.Input('请输入学生编号', font=('宋体', 20), size=(20, 4), key='-student_num_')],
              [sg.B('确认', key='-ok-', size=(15, 2)), sg.B('取消', key='-cancel-', size=(15, 2))]
        , [sg.B('返回首页', key='-return-', size=(32, 2))]]
    window_dp = sg.Window('根据编号删除学生', layout, resizable=True)
    ssi.deleteStudent(window_dp)


# 修改学生
def updateStudentByNum():
    layout = [[sg.Input('请输入学生编号', font=('宋体', 20), size=(20, 4), key='-student_num-')],
              [sg.Input('请输入学生姓名', font=('宋体', 20), size=(20, 4), key='-student_name-')],
              [sg.Input('请输入学生性别', font=('宋体', 20), size=(20, 4), key='-student_sex-')],
              [sg.Input('请输入学生年龄', font=('宋体', 20), size=(20, 4), key='-student_age-')],
              [sg.Input('请输入学生地址', font=('宋体', 20), size=(20, 4), key='-student_address-')],
              [sg.B('确认', key='-ok-', size=(15, 2)), sg.B('取消', key='-cancel-', size=(15, 2))]
        , [sg.B('返回首页', key='-return-', size=(32, 2))]]
    window_sd = sg.Window('修改学生信息', layout, resizable=True)
    ssi.updateStudent(window_sd)


# 登录
def login():
    layout = [[sg.Input('请输入用户名', font=('宋体', 20), size=(20, 4), key='-uname-')],
              [sg.Input('请输入密码', password_char="*", font=('宋体', 20), size=(20, 4), key='-key-')],
              [sg.B('登录', key='-login-', size=(10, 2)), sg.B('取消', key='-cancel-', size=(10, 2))]
              # sg.B('注册', key='-reg-', size=(10, 2))]
              ]
    window_lg = sg.Window('登录窗口', layout, resizable=True)
    usi.loginService(window_lg)

# # 注册（在数据库中增加用户）
# def reg():
#     while True:
#         layout = [[sg.Input('请输入用户名', font=('宋体', 20), size=(20, 4), key='-uname-')],
#                   [sg.Input('请输入密码', font=('宋体', 20), size=(20, 4), key='-key-')],
#                   [sg.Input('请输入昵称', font=('宋体', 20), size=(20, 4), key='-nknm-')],
#                   [sg.B('确认', key='-ok-', size=(10, 2)), sg.B('取消', key='-cancel-', size=(10, 2))]
#                   ]
#         window_rg = sg.Window('注册新账户', layout, resizable=True)
#         while True:
#             event, values = window_rg.read()  # 窗口的读取，有两个返回值(1.事件  2.值)
#             if event == '-ok-':
#                 newuser = values['-uname-']
#                 newpass = values['-key-']
#                 newname = values['-nknm-']
#                 sql = "SELECT * FROM users WHERE username='" + newuser + "';"
#                 data = orm.getData(sql)
#                 if len(data) != 0:
#                     sg.popup("此用户已注册")
#                 else:
#                     sql = "INSERT INTO users(id,username,password,nickname) VALUES(null,'" + str(newuser) + "','" + str(
#                         newpass) + "','" + str(newname) + "'); "
#                     # print(sql)
#                     orm.writeData(sql)
#                     window_rg.close()
#                     sg.popup("新用户添加成功！")
#                     login()
#             if event == '-cancel-':
#                 break
